package com.sky.control;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.BaseException;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Tag(name = "企业员工相关", description = "用于企业员工相关")//替换 @Api
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @Operation(summary = "员工登录",// 替换 @ApiOperation 的 value
            description = "这是一个员工登录相关接口")
    @ApiResponse(responseCode = "200", description = "成功找到用户") // 包名变了！
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("登录员工：{}", "" + employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);

    }

    @PostMapping("/logout")
    @Operation(summary = "员工退出登录", description = "这是一个员工退出登录相关接口")
    public Result<String> logout(
            @Parameter(description = "用户名username", required = true, example = "admin")
            @RequestParam String username) {
        return Result.success("删除成功");

    }

    @PostMapping
    @Operation(summary = "添加员工")
    public Result save(@RequestBody EmployeeDTO employeeDto) {
        log.info("新增员工：{}", employeeDto.getUsername());

        employeeService.save(employeeDto);
        return Result.success();

    }

    @PostMapping("/page")
    @Operation(summary = "员工分页查询")
    public Result<PageResult> page(@RequestBody EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询，参数为：{}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);

        return Result.success(pageResult);

    }

    @DeleteMapping
    @Operation(summary = "删除员工")
    public Result delete(@RequestBody EmployeeDTO employeeDTO) {
        log.info("删除员工：{}", employeeDTO);
        employeeService.delete(employeeDTO);
        return Result.success();
    }

    @GetMapping("/{userId}")
    @Operation(summary = "根据id查询员工信息")
    public Result<Employee> getEmployeeById(@PathVariable("userId") Long id){
        log.info("根据ID获取员工信息：{}",id);
        if (id==null||!(id instanceof Long)){
            throw  new BaseException(MessageConstant.ARGUMENTS_NULL);
        }

        return Result.success(employeeService.getEmployeeById(id));
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启用禁用员工账号")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用员工账号：{},{}", status, id);
        String msg = employeeService.startOrStop(status, id);
        return Result.success(msg);
    }

    @PostMapping("/update")
    @Operation(summary = "编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：{}",employeeDTO);
        if(employeeDTO==null){
            throw new BaseException(MessageConstant.ARGUMENTS_NULL);
        }

        employeeService.update(employeeDTO);
        return Result.success();
    }
}


